package com.radroxx.balancelg;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.app.PendingIntent;
import android.widget.RemoteViews;

import com.radroxx.balancelg.balancedata.GetBalance;

public class Widget extends AppWidgetProvider {

    private GetBalance getBalance[] = null ;
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
		super.onUpdate(context,appWidgetManager,appWidgetIds);

        for( int i : appWidgetIds)
        {
			GetBalance g = GetBalance.getBalance( context , i );
            if( g != null )
                updateWidget(context,appWidgetManager,i,g);
        }
	}
    @Override
    public void onDeleted(Context context,int[] appWidgetIds){
        for( int i : appWidgetIds )
        {
            GetBalance g = GetBalance.getBalance( context , i);
            if( g != null )
                g.Delete( context );
        }
    }

    private GetBalance popGetBalance( int widgetId ){
        if( getBalance != null )
        {
            for( int i = 0 ; i < getBalance.length ; ++i )
            {
                if( getBalance[i] != null )
                {
                    if( getBalance[i].getWidgetId() == widgetId )
                    {
                        GetBalance rez = getBalance[i] ;
                        getBalance[i] = null;
                        return rez;
                    }
                }
            }
        }
        return null;
    }

    public static void updateWidget( Context context , AppWidgetManager appWidgetManager , int widgetId , GetBalance gb ){
        appWidgetManager.updateAppWidget( widgetId , new RemoteViews( context.getPackageName() , R.layout.widgetwait ) );

        PendingIntent pIntent = null ;

        /*
        // Конфигурационный экран (первая зона)
        Intent configIntent = new Intent(context, ConfigWidget.class);
        configIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
        configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
        pIntent = PendingIntent.getActivity(context, widgetId, configIntent, 0);
        widgetView.setOnClickPendingIntent(R.id.progressBar, pIntent);
        */

        RemoteViews widgetView = new RemoteViews( context.getPackageName() , R.layout.widget );
        Intent updateIntent = new Intent(context, Widget.class);
        updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,new int[] { widgetId });
        pIntent = PendingIntent.getBroadcast(context, widgetId, updateIntent, 0);
        widgetView.setOnClickPendingIntent(R.id.lb_data, pIntent);
        //Обновление данных

        String dataNumeric = gb.getData( context ) ;
        widgetView.setTextViewText( R.id.lb_data, gb.getProvider().name() + "\n" + gb.getLogin() + "\n" + dataNumeric + "\n" + gb.getUpdateTimeString() );
        gb.Save(context);

        appWidgetManager.updateAppWidget( widgetId , widgetView );
    }
}
